﻿using System;
using System.Collections.Generic;
using System.Linq;
using libtcod;

namespace RL
{
    public interface IRenderable
    {
        TCODColor Color { get; }
        char Model { get; }
    }

    class Program
    {
        private static bool _quit;
        private static readonly GraphicsEngine view = new GraphicsEngine();
        const int MapLength = 200;
        private const int MapWidth = 200;
        private static Random _random;
        static void Main()
        {

            using (var game = new SettlersRL())
            {
                game.Run();
            }

            return;

            _random = new Random();
            IInputEngine input = new InputEngine();
            GameMap world = new GameMap(MapLength, MapWidth);

            Entity player = new Entity(new Point(), '@', TCODColor.white);
            Entity npc = new Entity(new Point(5, 5), 'A', TCODColor.yellow);
            List<IEntity> pups = new List<IEntity>{player, npc};

            input.RegisterInterest(TCODKeyCode.Up, () => player.Move(MoveType.Up));
            input.RegisterInterest(TCODKeyCode.Down, () => player.Move(MoveType.Down));
            input.RegisterInterest(TCODKeyCode.Left, () => player.Move(MoveType.Left));
            input.RegisterInterest(TCODKeyCode.Right, () => player.Move(MoveType.Right));
            input.RegisterInterest(TCODKeyCode.Escape, () => _quit = true);
            input.RegisterInterest(TCODKeyCode.F12, () => view.ToggleFullScreen());

            //input.RegisterInterest(TCODMouseCode.LeftButton, x=>player.MoveTo(new Point(x.CellX, x.CellY)));
            input.RegisterInterest(TCODMouseCode.Movement, x=>view.DrawToPanel(string.Format("Mouse movement: ({0},{1})", x.CellX, x.CellY)));

            Rect viewWindow = null;
            while (!_quit)
            {
                input.Run();
                viewWindow = ComputeViewWindow(player.Body.Position, GraphicsEngine.MAIN_WIDTH, GraphicsEngine.MAIN_HEIGHT);
                view.DrawToMain(world.MapConsole, viewWindow);
                view.DrawToPanel(TCODSystem.getFps().ToString());
                view.DrawToPanel(player.Body.Position.ToString());
                view.DrawToPanel(viewWindow.ToString());

                view.RenderToScreen();
            }
        }

        private static Rect ComputeViewWindow(Point center, int w, int h)
        {

            return new Rect(Math.Max(center.X - GraphicsEngine.MAIN_WIDTH / 2, 0), Math.Max(center.Y - GraphicsEngine.MAIN_HEIGHT / 2,0), w, h);
        }

        private static Point RandomPoint(int mapLength, int mapWidth)
        {            
            return new Point(_random.Next() % mapLength, _random.Next() % mapWidth);
        }

        private static void HandleMouse()
        {
            var status = TCODMouse.getStatus();
            view.DrawToPanel(string.Format("X:{0} Y:{1} LB:{2} RB:{3}", status.CellX, status.CellY, status.LeftButton, status.RightButtonPressed));
        }
    }
}
